/*
 * ViewFullImageAction.java
 *
 * Created on 14 czerwiec 2007, 13:04
 *
 */

package arocms.gallery;

import arocms.business.ImageDAO;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author arek
 */
public class ViewFullImageAction extends  Action{
    
    private static final String FAILURE = "failure";
    private static final String SUCCESS = "success";
    private Logger log = Logger.getLogger(this.getClass());
    
    /** Creates a new instance of ViewFullImageAction */
    public ViewFullImageAction() {
    }
    
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            Long id = Long.valueOf(request.getParameter("id"));
            InputStream imageIn = new ImageDAO().getImageData(id);
            BufferedImage fullSizeImage = ImageIO.read(imageIn); //pobranie obrazu z bazy danych
            response.setContentType("image/jpeg");
            ImageIO.write(fullSizeImage, "jpeg", response.getOutputStream());
            imageIn.close();
            response.getOutputStream().flush();
            return null;
        } catch (NumberFormatException ex) {
            log.error(ex.toString());
        }
        return mapping.findForward(FAILURE);
    }
    
}
